package com.yunshipei.core.ui.view;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.yunshipei.core.model.XCloudJsModel;
import com.yunshipei.core.ui.client.FaceWebChromeClient;

import org.json.JSONException;
import org.json.JSONObject;

public class FaceWebView extends WebView {

    public FaceWebView(Context context) {
        super(context);
        privateInit();
    }

    public FaceWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        privateInit();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void privateInit() {
        setHorizontalScrollBarEnabled(false);
        WebSettings settings = getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSaveFormData(true);
        settings.setGeolocationEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setDomStorageEnabled(true);
        settings.setAppCacheMaxSize(1024 * 1024 * 8);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setSupportMultipleWindows(true);
    }

    private FaceWebChromeClient faceWebChromeClient;

    public void setFaceWebChromeClient(FaceWebChromeClient client) {
        super.setWebChromeClient(client);
        faceWebChromeClient = client;
    }

    public FaceWebChromeClient getFaceWebChromeClient() {
        return faceWebChromeClient;
    }

    public boolean forwardHistory() {
        if (canGoForward()) {
            goForward();
            return true;
        }
        return false;
    }

    public boolean backHistory() {
        if (canGoBack()) {
            goBack();
            return true;
        }
        return false;
    }

    public void loadAdapter(String mAdapter, String target, boolean hasAutoLogin) {
        loadUrl("javascript:yspApp.notice('loadModel',{modelURL:'xcloud2json/" + mAdapter.replace("adapter://", "") + "',targetURL:'" + target + "',hasAutoLogin:" + hasAutoLogin + "})");
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void loadJsBack(ValueCallback<String> valueCallback) {
        evaluateJavascript("yspApp.notice('back')", valueCallback);
    }

    public void loadJsNetConnected(boolean isWifi) {
        loadUrl("javascript:yspApp.notice('netConnected',{netType:'" + (isWifi ? "TYPE_WIFI" : "TYPE_MOBILE") + "'})");
    }

    public void loadJsNetDisConnected() {
        loadUrl("javascript:yspApp.notice('netDisconnected')");
    }

    public void loadJsTimeOut() {
        loadUrl("javascript:yspApp.notice('timedOut')");
    }

    public void loadJsLocation(String uuid, String location) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uuid", uuid);
            jsonObject.put("address", location);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        loadUrl("javascript: setLocation(" + jsonObject + ");");
    }

    public void loadJsSendImage(String data) {
        loadUrl("javascript: setImageData('" + data + "');");
    }

    @SuppressLint("JavascriptInterface")
    public void addTabJavascriptInterface(XCloudJsModel... xCloudJsModel) {
        XCloudJsModel[] xCloudJsModels = xCloudJsModel.clone();
        for (XCloudJsModel xCloudJs : xCloudJsModels) {
            if (xCloudJs != null) {
                addJavascriptInterface(xCloudJs.getObject(), xCloudJs.getObjName());
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void loadJsRefresh(ValueCallback<String> valueCallback) {
        evaluateJavascript("yspApp.notice('refresh');", valueCallback);
    }
}
